package View;

import java.awt.EventQueue;
import java.awt.TextArea;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.ButtonGroup;

import Controller.DB;
import Model.Customer;
import Model.MenuItem;

public class ReviewPopUp extends JFrame {

	Customer customer;
	MenuItem item;
	JTextArea textArea;
	
	private final ButtonGroup buttonGroup = new ButtonGroup();
	/**
	 * Create the frame.
	 */
	public ReviewPopUp(Customer cust, MenuItem itm) {
		this.setTitle("Review " + itm.getName());
		customer = cust;
		item = itm;
		setBounds(100, 100, 450, 300);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		textArea = new JTextArea();
		textArea.setText("Please leave a comment here");
		textArea.setWrapStyleWord(true);
		textArea.setBounds(65, 94, 283, 99);
		textArea.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JTextArea txt = (JTextArea) e.getSource();
				if (txt.getText().equals("Please leave a comment here")) {
					txt.setText("");
				}
			}
		});
		panel.add(textArea);
		
		
		JRadioButton rdbtnNewRadioButton = new JRadioButton("1");
		rdbtnNewRadioButton.setActionCommand("1");
		buttonGroup.add(rdbtnNewRadioButton);
		rdbtnNewRadioButton.setVerticalTextPosition(JRadioButton.BOTTOM);
		rdbtnNewRadioButton.setHorizontalTextPosition(JRadioButton.CENTER);
		rdbtnNewRadioButton.setBounds(47, 34, 28, 37);
		panel.add(rdbtnNewRadioButton);
		
		JRadioButton radioButton = new JRadioButton("2");
		buttonGroup.add(radioButton);
		radioButton.setActionCommand("2");
		radioButton.setVerticalTextPosition(SwingConstants.BOTTOM);
		radioButton.setHorizontalTextPosition(SwingConstants.CENTER);
		radioButton.setBounds(122, 34, 28, 37);
		panel.add(radioButton);
		
		JRadioButton radioButton_1 = new JRadioButton("3");
		buttonGroup.add(radioButton_1);
		radioButton_1.setActionCommand("3");
		radioButton_1.setVerticalTextPosition(SwingConstants.BOTTOM);
		radioButton_1.setHorizontalTextPosition(SwingConstants.CENTER);
		radioButton_1.setBounds(197, 34, 28, 37);
		panel.add(radioButton_1);
		
		JRadioButton radioButton_2 = new JRadioButton("4");
		radioButton_2.setActionCommand("4");
		buttonGroup.add(radioButton_2);
		radioButton_2.setVerticalTextPosition(SwingConstants.BOTTOM);
		radioButton_2.setHorizontalTextPosition(SwingConstants.CENTER);
		radioButton_2.setBounds(272, 34, 28, 37);
		panel.add(radioButton_2);
		
		JRadioButton radioButton_3 = new JRadioButton("5");
		radioButton_3.setActionCommand("5");
		buttonGroup.add(radioButton_3);
		radioButton_3.setVerticalTextPosition(SwingConstants.BOTTOM);
		radioButton_3.setHorizontalTextPosition(SwingConstants.CENTER);
		radioButton_3.setBounds(347, 34, 28, 37);
		panel.add(radioButton_3);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(65, 219, 117, 29);
		btnSubmit.addActionListener(new SubmitAction());
		panel.add(btnSubmit);
		
		JButton cancel = new JButton("Cancel");
		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				((JFrame) ((JButton) e.getSource()).getTopLevelAncestor()).dispose();
				
			}
			
		});
		cancel.setBounds(231, 219, 117, 29);
		panel.add(cancel);
		
		this.setLocationRelativeTo(null);
		this.setVisible(true);

	}
	
	public int getRating() {
		Enumeration<AbstractButton> allBtns = buttonGroup.getElements();
		
		while (allBtns.hasMoreElements()) {
			JRadioButton btn = (JRadioButton) allBtns.nextElement();
			if (btn.isSelected()) {
				
				return Integer.parseInt(btn.getActionCommand());
			}
		}
		return 0;
	}
	
	public String getComment() {
		if (!textArea.getText().equals("Please leave a comment here")) {
			return textArea.getText();
		} else {
			return null;
		}
	}
	
	private class SubmitAction implements ActionListener {


		@Override
		public void actionPerformed(ActionEvent e) {
			int rating = getRating();
			String comment = getComment();
			if (rating != 0) {

				String sql = "INSERT INTO _445team11.Review(reviewDate, reviewComments, reviewRating,Customer_userID, Item_itemID)  VALUES " +
						" ('" + new Date(System.currentTimeMillis()).toString() + "','" 
						+ comment +"','" + rating +"','" + customer.getUserID() + "','" + Integer.toString(item.getItemID()) + "') ON DUPLICATE KEY"
						+ " UPDATE reviewDate = '" +  new Date(System.currentTimeMillis()).toString() + 
						"', reviewComments = '" + comment + "', reviewRating = '" + rating +"'";
				try {
					Statement s = DB.conn.createStatement();
					s.executeUpdate(sql);
					((JFrame) ((JButton) e.getSource()).getTopLevelAncestor()).dispose();
				} catch (SQLException sqle) {
					sqle.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(null, "Please rate this item to continue");
			}

		}

	}

}
